package org.ne.springboot.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.ne.springboot.utils.PaperHandleUtils.PaperContentCheckUtils.ReportGenerateUtils;
import org.ne.springboot.utils.PaperHandleUtils.ReferenceCheckUtils.ReferenceCheckUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.ne.springboot.utils.PaperHandleUtils.AbstractTranslateCheckUtils.TranslateCheckUtils;
import org.ne.springboot.utils.PaperHandleUtils.StructCheckUtils.StructCheckUtils;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import static org.ne.springboot.utils.PaperHandleUtils.PaperContentCheckUtils.PaperHandleUtils.contentAnalyze;
import static org.ne.springboot.utils.PaperHandleUtils.PaperContentCheckUtils.PaperHandleUtils.fontCheckReport;
/**
 * @author wey
 * @date 2025/8/25 下午8:46 08
 **/


@RestController
@RequestMapping("/paper")
public class PaperHandleController {

    private final ReportGenerateUtils rgu = new ReportGenerateUtils();





    /**
     * 处理并分析DOCX文档
     *
     * <p>根据指定的问题对上传的DOCX文档进行处理分析，并生成检查报告。</p>
     * <p>报告通常以HTTP响应形式返回，例如生成PDF或Excel格式的文件供用户下载。</p>
     *
     * @param response HttpServletResponse对象，用于设置响应头、输出流等，以向客户端返回生成的报告
     * @param question 字符串类型，表示需要分析的具体问题或指令
     * @param file 用户上传的DOCX文档文件
     * @throws IOException 当文件读取、处理或写入响应输出流过程中发生I/O错误时抛出
     *
     */
    @PostMapping("/analyze")
    public void analyzeDocx(HttpServletResponse response, @RequestParam("question") String question, @RequestParam("docx") MultipartFile file, @RequestParam("model") String model) throws IOException {


        List<Map<String, Object>> res = contentAnalyze(question, file, model);
        rgu.generatePaperCheckReport(response, res, file);
    }


    @PostMapping("/fontCheckAndReport")
    public void fontCheckAndReport(@RequestParam("docx") MultipartFile file, HttpServletResponse response) throws Exception {
        fontCheckReport(file, response);
    }




    @PostMapping("/structCheck")
    public void structCheck(HttpServletResponse response, @RequestParam("docx") MultipartFile file) throws IOException {

        try (XWPFDocument document = StructCheckUtils.structCheckByAI(response, file); OutputStream os = response.getOutputStream()) {
            document.write(os);
        }
    }

    @PostMapping("/translateCheck")
    public void translateCheck(HttpServletResponse response, @RequestParam("docx") MultipartFile file, @RequestParam("model") String model) throws IOException {

        try (XWPFDocument document = TranslateCheckUtils.translate(response, file, model); OutputStream os = response.getOutputStream()) {
            document.write(os);
        }
    }

    @PostMapping("/referenceCheck")
    public void referenceCheck(HttpServletResponse response, @RequestParam("docx") MultipartFile file, @RequestParam("model") String model) throws IOException {
        try (XWPFDocument document = ReferenceCheckUtils.check(file, model); OutputStream os = response.getOutputStream()) {
            document.write(os);
        }

    }

}
